//
//  TodoViewController.swift
//  memo
//
//  Created by fly on 2019/9/28.
//  Copyright © 2019 Xiang HongFei. All rights reserved.
//

import UIKit

class TodoViewController: UITableViewController ,AddItemViewControllerDelegate{
    var category : TodoCategory!
    
    required init?(coder aDecoder: NSCoder) {
   
        
        
        super.init(coder: aDecoder)
        
 
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let category = category{
            title = category.name
        }
      
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return category.items.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodoListItem",for: indexPath)
        let textLabel = cell.viewWithTag(1000) as! UILabel
        let repertory = cell.viewWithTag(1002) as! UILabel
        
        let item = category.items[indexPath.row]
        textLabel.text = item.text
        repertory.text = "当前库存：\(item.repertory)"
        configCellChecknark(for: cell, at: indexPath)
        return cell
    }
    
    func configCellChecknark(for cell:UITableViewCell,at indexPath: IndexPath){
        let checkLable = cell.viewWithTag(1001) as! UILabel
        let item = category.items[indexPath.row]
        if item.checked {
            checkLable.text = ""
        } else {
            checkLable.text = ""
        }
        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50 
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            let item = category.items[indexPath.row]
            item.checked = !item.checked
            configCellChecknark(for: cell, at: indexPath)
            
            tableView.deselectRow(at: indexPath, animated: true)
        }

    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        category.items.remove(at: indexPath.row)
        
        let path = [indexPath]
        tableView.deleteRows(at: path, with: .automatic)

    }
    
    func AddItemViewController(_controller: AddItemViewController, endAdding item: TodoItem) {
        let newRow = category.items.count
        category.items.append(item)
        
        let indexPath = IndexPath(row: newRow, section: 0)
        let Pates = [indexPath]
        tableView.insertRows(at: Pates, with: .automatic)
   
    }
    func AddItemViewController(_controller: AddItemViewController, endEditing item: TodoItem) {
        if let index = category.items.firstIndex(where: {$0 === item}){
            category.items[index] = item
            
            let indexPath = IndexPath(row: index, section: 0)
            if let cell = tableView.cellForRow(at: indexPath) {
                let textLabel = cell.viewWithTag(1000) as! UILabel
                let repertory = cell.viewWithTag(1002) as! UILabel
                textLabel.text = item.text
                repertory.text = "当前库存：\(item.repertory)"
            }
            
        }
        
  
    }
    func AddItemViewController(_controller: AddItemViewController, enddelete item: TodoItem) {
        if let index = category.items.firstIndex(where: {$0 === item}){
            let indexPath = IndexPath(row: index, section: 0)
            
            category.items.remove(at: indexPath.row)
            
            let path = [indexPath]
            tableView.deleteRows(at: path, with: .automatic)
        }
     }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddItem" {
            let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegatr = self
        }else if segue.identifier == "EditItem" {
            let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegatr = self
            if let index = tableView.indexPath(for: sender as! UITableViewCell){
                addItemViewController.itemToEdit =  category.items[index.row]
            }
        }
    }
}
